from decimal import  Decimal
from math import floor, ceil
import datetime
import os

def round_to(value: float, target: float) -> float:
    """
    Round price to price tick value.
    """
    value = Decimal(str(value))
    target = Decimal(str(target))
    rounded = float(int(round(value / target)) * target)
    return rounded


def floor_to(value: float, target: float) -> float:
    """
    Similar to math.floor function, but to target float number.
    """
    value = Decimal(str(value))
    target = Decimal(str(target))
    result = float(int(floor(value / target)) * target)
    return result


def ceil_to(value: float, target: float) -> float:
    """
    Similar to math.ceil function, but to target float number.
    """
    value = Decimal(str(value))
    target = Decimal(str(target))
    result = float(int(ceil(value / target)) * target)
    return result


def convert_timestamp(x):
    try:
        x = x/1000
        dateArray = datetime.datetime.fromtimestamp(x)
        otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S") # 2013-10-10 23:40:00
        return otherStyleTime
    except Exception as e:
        print(x)

def generatefile(path):
    names = []
    for dirpath, dirnames, filenames in os.walk(path):
        names = filenames
    return names


def generate_months(date:datetime, start_date = datetime.datetime(year=2017,month=8,day=1)):
    current_year = date.year
    current_month = date.month
    year = start_date.year
    month = start_date.month
    while not (year >= current_year and month >= current_month):
        month_str = "0"+str(month) if month <10 else str(month)
        yield f"{year}-{month_str}"
        if month <12:
            month = month +1
        else:
            month = 1
            year = year + 1

def generate_dates (date:datetime):
    year = date.year
    month = date.month
    current_day = date.day
    day = 1
    while day <current_day:
        day_str = "0"+str(day) if day <10 else str(day)
        month_str = "0"+str(month) if month <10 else str(month)
        yield f"{year}-{month_str}-{day_str}"
        day = day + 1